import axios from "axios";
import router from "@/router";
import Cookies from "js-cookie"
import {Notification} from 'element-ui'

const api = axios.create({
  baseURL:'http://localhost:8056',
  timeout: 3000
})

api.interceptors.request.use(function (config){
  const token = Cookies.get('Token')
  config.headers.set('satoken', token ?? '')
  return config;
})

api.interceptors.response.use(
  response => {
    // 如果返回的状态码为1，说明接口请求成功，可以正常拿到数据
    // 否则的话抛出错误
    if (response.data.code === 0) {
      return response.data;
    } else {
        if (response.data.code===16002||response.data.code===16003){
            router.push('/views/manage/login/loginHome')
        }
    }
  },
  error => {
    Notification({type: 'error', title: '错误', message: error.response.data.error, customClass: 'http-message'})
  }
)
export default api
